
package niconama04.model.impl;

import java.util.List;

import niconama04.model.Transacter;
import niconama04.shared.data.Category;
import niconama04.shared.data.CategoryOriginal;
import niconama04.shared.data.Grammer;
import niconama04.shared.data.GrammerWord;
import niconama04.shared.data.Original;
import niconama04.shared.data.OriginalUser;
import niconama04.shared.data.Sentence;
import niconama04.shared.data.User;
import niconama04.shared.data.Word;


class TransacterImpl implements Transacter {

	private final Dao dao;

	TransacterImpl(Dao x_dao) {
		dao = x_dao;
	}

	@Override
	public Sentence registSentence(Sentence x_sentence) {
		if (x_sentence == null) {
			throw new IllegalArgumentException("[x_sentence:Sentence](argment at 0) is null.");
		}
		return prv_registSentence(x_sentence, null);
	}

	@Override
	public Sentence registSentence(Sentence x_sentence, User x_user) {
		if (x_sentence == null) {
			throw new IllegalArgumentException("[x_sentence:Sentence](argment at 0) is null.");
		}
		if (x_user == null) {
			throw new IllegalArgumentException("[x_user:User](argment at 1) is null.");
		}
		return prv_registSentence(x_sentence, x_user);
	}

	private Sentence prv_registSentence(Sentence x_sentence, User x_user) {
		Grammer grammer = x_sentence.getGrammer();
		List<Word> words = x_sentence.getWords();
		if (x_user != null) {
			List<User> entitys = dao.getUsers(x_user.getTwitterId());
			if (entitys.isEmpty()) {
				dao.registUser(x_user);
			} else {
				User entity = entitys.get(0);
				x_user.setKey(entity.getKey());
			}
		}
		{
			List<Grammer> entitys = dao.getGrammers(grammer.getConfiguration());
			if (entitys.isEmpty()) {
				dao.registGrammer(grammer);
			} else {
				Grammer entity = entitys.get(0);
				grammer.setKey(entity.getKey());
			}
		}
		for (int i = 0; i < words.size(); i++) {
			Word word = words.get(i);
			Original original = word.getOriginal();
			{
				List<Original> entitys = dao.getOriginals(original.getPos(), original.getBaseform());
				if (entitys.isEmpty()) {
					dao.registOriginal(original);
				} else {
					Original entity = entitys.get(0);
					original.setKey(entity.getKey());
				}
				word.setOriginalKey(original.getKey());
			}
			{
				List<Word> entitys = dao.getWords(word.getPosKey(), word.getSurface(), word.getBaseform());
				if (entitys.isEmpty()) {
					dao.registWord(word);
				} else {
					Word entity = entitys.get(0);
					word.setKey(entity.getKey());
				}
			}
			if (x_user != null) {
				List<OriginalUser> es = dao.getOriginalUsers(original.getKey(), x_user.getKey());
				if (es.isEmpty()) {
					OriginalUser ou = new OriginalUser();
					ou.setOriginalKey(original.getKey());
					ou.setUserKey(x_user.getKey());
					dao.registOriginalUser(ou);
				}
			}
			{
				List<GrammerWord> es = dao.getGrammerWords(grammer.getKey(), word.getKey(), i);
				if (es.isEmpty()) {
					GrammerWord gw = new GrammerWord();
					gw.setGrammerKey(grammer.getKey());
					gw.setWordKey(word.getKey());
					gw.setIndex(i);
					dao.registGrammerWord(gw);
				}
			}
		}
		return x_sentence;
	}

	@Override
	public Category registCategory(Category x_category) {
		if (x_category == null) {
			throw new IllegalArgumentException("[x_category:Category](argment at 0) is null.");
		}
		return prv_registCategory(x_category, null);
	}

	@Override
	public Category registCategory(Category x_category, User x_user) {
		if (x_category == null) {
			throw new IllegalArgumentException("[x_category:Category](argment at 0) is null.");
		}
		if (x_user == null) {
			throw new IllegalArgumentException("[x_user:User](argment at 1) is null.");
		}
		return prv_registCategory(x_category, x_user);
	}

	private Category prv_registCategory(Category x_category, User x_user) {
		if (x_user != null) {
			List<User> entitys = dao.getUsers(x_user.getTwitterId());
			if (entitys.isEmpty()) {
				dao.registUser(x_user);
			} else {
				User entity = entitys.get(0);
				x_user.setKey(entity.getKey());
			}
		}
		{
			List<Category> entitys = dao.getCategorys(x_category.getName());
			if (entitys.isEmpty()) {
				dao.registCategory(x_category);
			} else {
				Category entity = entitys.get(0);
				x_category.setKey(entity.getKey());
			}
		}
		return x_category;
	}

	@Override
	public CategoryOriginal registCategoryOriginal(CategoryOriginal x_categoryOriginal) {
		Category category = dao.getCategory(x_categoryOriginal.getCategoryKey());
		if (category == null) {
			return null;
		}
		Original original = dao.getOriginal(x_categoryOriginal.getOriginalKey());
		if (original == null) {
			return null;
		}
		List<CategoryOriginal> cos = dao.getCategoryOriginals(x_categoryOriginal.getCategoryKey(), x_categoryOriginal.getOriginalKey());
		if (cos.isEmpty()) {
			dao.registCategoryOriginal(x_categoryOriginal);
		} else {
			CategoryOriginal entity = cos.get(0);
			x_categoryOriginal.setKey(entity.getKey());
		}
		return x_categoryOriginal;
	}

	@Override
	public void deleteCategoryOriginal(CategoryOriginal x_categoryOriginal) {
		dao.deleteCategoryOriginal(x_categoryOriginal);
	}
}
